package com.luhan.fmmall.controller;

import com.github.wxpay.sdk.WXPay;
import com.luhan.fmmall.config.MyPayConfig;
import com.luhan.fmmall.entity.Orders;
import com.luhan.fmmall.service.OrderService;
import com.luhan.fmmall.vo.ResStatus;
import com.luhan.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "提供订单信息相关接口",tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("添加订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cids",dataType = "string",required = true,value = "购物车id集合"),
            @ApiImplicitParam(name = "order",dataType = "Orders",required = true,value = "订单对象")
    })
    @PostMapping("/add")
    public ResultVO add(String cids,
                        @RequestBody Orders order,
                        @RequestHeader("token") String token){
        ResultVO resultVO = null;
        try {
            Map<String, String> orderInfo = orderService.addOrder(cids, order);
            String orderId = orderInfo.get("orderId");
            if (orderInfo!=null){
                //设置当前订单信息
                HashMap<String,String> data = new HashMap<>();
                //商品描述
                data.put("body",orderInfo.get("productNames"));
                //使用当前用户的订单编号作为当前支付的交易号
                data.put("out_trade_no",orderId);
                //支付币种
                data.put("fee_type","CNY");
                //交易类型
                data.put("trade_type","NATIVE");
                //支付金额
                //data.put("total_fee",order.getActualAmount()*100+"");
                data.put("total_fee","1");
                //设置支付完成时的回调方法接口
                data.put("notify_url","http://hcl.vipgz1.91tunnel.com/pay/callback");

                //发送请求
                //微信支付：申请支付链接
                WXPay wxPay = new WXPay(new MyPayConfig());
                Map<String, String> resp = wxPay.unifiedOrder(data);
                orderInfo.put("payUrl",resp.get("code_url"));
                //System.out.println(resp);
                //orderInfo中包含：订单编号，后买商品的名称，支付链接
                resultVO = new ResultVO(ResStatus.OK, "提交订单成功！", orderInfo);
            }else {
                resultVO = new ResultVO(ResStatus.NO, "提交订单失败！", null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resultVO = new ResultVO(ResStatus.NO, "提交订单失败！", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultVO;
    }

    @ApiOperation("查询订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",dataType = "string",required = true,value = "用户id"),
            @ApiImplicitParam(name = "pageNum",dataType = "int",required = true,value = "当前页码"),
            @ApiImplicitParam(name = "limit",dataType = "int",required = true,value = "每页的条数"),
            @ApiImplicitParam(name = "status",dataType = "string",required = false,value = "订单状态")
    })
    @GetMapping("/list")
    public ResultVO list(@RequestHeader("token")String token,
                         String userId,int pageNum,int limit,String status){
        ResultVO resultVO = orderService.listOrder(userId, pageNum, limit, status);
        return resultVO;
    }
}
